﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ThreadModule.Data
{
    /// <summary>
    /// 螺纹计算类，用于存储中间计算的参数值
    /// </summary>
    public class CalculatedThreadParameter
    {
        /// <summary>
        /// 位置(上/下)
        /// </summary>
        [JsonIgnore]
        public Position position { get; set; }

        /// <summary>
        /// 螺距参数集合(各个牙)
        /// </summary>
        [JsonProperty("所有螺距参数")]
        public List<double> listPitch { get; set; }

        /// <summary>
        /// 半角参数集合
        /// </summary>
        [JsonProperty("半角参数")]
        public List<double> listHalfAngle { get; set; }

        /// <summary>
        /// 左半角参数
        /// </summary>
        [JsonProperty("左半角参数")]
        public double leftHalfAngle { get; set; }

        /// <summary>
        /// 右半角参数
        /// </summary>
        [JsonProperty("右半角参数")]
        public double rightHalfAngle { get; set; }

        /// <summary>
        /// 全角参数
        /// </summary>
        [JsonProperty("全角参数")]
        public double fullAngle { get; set; }

        /// <summary>
        /// 螺距参数
        /// </summary>
        [JsonProperty("螺距参数")]
        public double pitch { get; set; }

        /// <summary>
        /// 大径参数
        /// </summary>
        [JsonProperty("大径参数")]
        public Tuple<double, double> paramMajor { get; set; }

        /// <summary>
        /// 大径拟合点
        /// </summary>
        [JsonProperty("大径拟合点")]
        public List<RawDataPoint> listFitMajorPoints { get; set; }

        /// <summary>
        /// 小径参数
        /// </summary>
        [JsonProperty("小径参数")]
        public Tuple<double, double> paramMinor { get; set; }

        /// <summary>
        /// 小径拟合点
        /// </summary>
        [JsonProperty("小径拟合点")]
        public List<RawDataPoint> listFitMinorPoints { get; set; }

        /// <summary>
        /// 中径参数
        /// </summary>
        [JsonProperty("中径参数")]
        public Tuple<double, double> paramPitchDiameter { get; set; }

        /// <summary>
        /// 全角参数集合
        /// </summary>
        [JsonIgnore]
        public List<Tuple<double, double>> listFullAngleParameters { get; set; }

        /// <summary>
        /// 拟合牙侧角数据点集合
        /// </summary>
        [JsonIgnore]
        public List<List<RawDataPoint>> listFitAnglePoints { get; set; }
        
        /// <summary>
        /// 计算参数类构造函数
        /// </summary>
        public CalculatedThreadParameter()
        {
            listPitch = new List<double>();
            listHalfAngle = new List<double>();
            paramMajor = new Tuple<double, double>(0, 0);
            paramMinor = new Tuple<double, double>(0, 0);
            paramPitchDiameter = new Tuple<double, double>(0, 0);
            listFullAngleParameters = new List<Tuple<double, double>>();
        }

        /// <summary>
        /// 输出螺距的平均值
        /// </summary>
        /// <returns></returns>
        public double GetPitch()
        {
            this.pitch = this.listPitch.Average();
            return this.pitch;
        }

        /// <summary>
        /// 计算全角值
        /// </summary>
        /// <returns></returns>
        public double GetFullAngle()
        {
            return (this.leftHalfAngle + this.rightHalfAngle) / 2;
        }

        /// <summary>
        /// 计算半角值
        /// </summary>
        /// <param name="isLeftAngle"></param>
        /// <returns></returns>
        public double GetHalfAngle(bool isLeftAngle)
        {
            int nStart = 0;
            int nCounter = 0;
            double halfAngle = 0.0;
            if (isLeftAngle)
            {
                nStart = 0;
                nCounter = listHalfAngle.Count;
            }
            else
            {
                nStart = 1;
                nCounter = listHalfAngle.Count - 1;
            }
            halfAngle = this.listHalfAngle.Skip(nStart).Take(nCounter).Where((n, index) => index % 2 == 0).Average();
            if (isLeftAngle)
            {
                leftHalfAngle = halfAngle;
            }
            else
            {
                rightHalfAngle = halfAngle;
            }
            //Console.WriteLine(AngleConverter.ConvertDecimalToDMS(halfAngle));
            return halfAngle;
        }

        /// <summary>
        /// 输出计算信息
        /// </summary>
        [Conditional("DEBUG")]
        public void Dump()
        {
            switch (position)
            {
                case Position.Top:
                    Console.WriteLine("\n上半部螺纹参数：");
                    break;
                case Position.Bottom:
                    Console.WriteLine("\n下半部螺纹参数：");
                    break;
            }
            string json = JsonConvert.SerializeObject(this, Formatting.Indented);
            Console.WriteLine(json);
        }
    }
}
